<template>
  <div class="app-container">

    <div>
      <el-button :loading="downloadLoading" style="margin:0 0 20px 20px;" type="primary" icon="el-icon-document" @click="handleDownload">
        导出
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading..." border fit highlight-current-row @selection-change="handleSelectionChange">
			<el-table-column type="selection" align="center" />
			<el-table-column prop="id" label="编号"  width="180"></el-table-column>
      <el-table-column prop="date" label="日期"  width="180"></el-table-column>
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="address" label="地址"> </el-table-column>
    </el-table>

		<upload-excel-component :on-success="handleSuccess" :before-upload="beforeUpload" />
    <el-table :data="tableData" border highlight-current-row style="width: 100%;margin-top:20px;">
      <el-table-column v-for="item of tableHeader" :key="item" :prop="item" :label="item" />
    </el-table>
  </div>
</template>

<script>

import UploadExcelComponent from '@/components/UploadExcel/index.vue'
export default {
  name: 'ExportExcel',
	components: { UploadExcelComponent },
  data() {
    return {
      list: [
				{id: 100,date: '2016-05-02',name: '王小虎',address: '上海市普陀区金沙江路 1518 弄'}
				,{id: 100,date: '2016-05-02',name: '王小虎',address: '上海市普陀区金沙江路 1518 弄'}
				,{id: 100,date: '2016-05-02',name: '王小虎',address: '上海市普陀区金沙江路 1518 弄'}
				,{id: 100,date: '2016-05-02',name: '王小虎',address: '上海市普陀区金沙江路 1518 弄'}
				,{id: 100,date: '2016-05-02',name: '王小虎',address: '上海市普陀区金沙江路 1518 弄'}
				],
			multipleSelection: [],		
      listLoading: true,
      downloadLoading: false,
      filename: '',
      autoWidth: true,
      bookType: 'xlsx',

			tableData: [],
      tableHeader: []
    }
  },
  created() {
		this.listLoading = false
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleDownload() {
			if (!this.multipleSelection.length) {
				this.$modal.msgWarning( '请至少选择一项')
				// this.$modal.msgWarning( 'Please select at least one item')
				return
			}
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['编号','日期', '姓名', '地址']
        const filterVal = ['id','date', 'name', 'address']
        const list = this.multipleSelection
        const data = this.formatJson(filterVal, list)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: this.filename,
          autoWidth: this.autoWidth,
          bookType: this.bookType
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        return v[j]
      }))
    },
		beforeUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1

      if (isLt1M) {
        return true
      }

      this.$message({
        message: 'Please do not upload files larger than 1m in size.',
        type: 'warning'
      })
      return false
    },
    handleSuccess({ results, header }) {
      this.tableData = results
      this.tableHeader = header
    }

  }
}
</script>

<style>

</style>
